package com.ayoka.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.List;

/**
 * Created by jinchunping on 2018/4/18.
 */

public class ProgressPieView extends View {

    private Paint piePaint;// 绘制pie的画笔
    private Paint circlePaint;// 绘制pie的画笔
    private Paint strokePaintWhite, strokePaintBackground;//绘制圆弧的画笔

    private int size;

    private int radius = 150;

    private int strokeWidth = 15;

    private int startAngles;

    private int[] colors = new int[]{Color.parseColor("#3eb3fe"), Color.parseColor("#ffbb62"), Color.parseColor("#FF3D3D"), Color.parseColor("#103D3D")};

    public ProgressPieView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        piePaint = new Paint();
        piePaint.setStyle(Paint.Style.FILL);
        piePaint.setAntiAlias(true);

        circlePaint = new Paint();
        circlePaint.setColor(Color.parseColor("#181837"));
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setAntiAlias(true);

        strokePaintWhite = new Paint();
        strokePaintWhite.setColor(Color.WHITE);
        strokePaintWhite.setStyle(Paint.Style.STROKE);
        strokePaintWhite.setAntiAlias(true);

        strokePaintBackground = new Paint();
        strokePaintBackground.setColor(Color.parseColor("#181837"));
        strokePaintBackground.setStyle(Paint.Style.FILL);
        strokePaintBackground.setAntiAlias(true);
    }


    @SuppressWarnings("unused")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        size = Math.min(width, height);
        radius = size / 2 - 5;
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(size / 2, size / 2);
        strokePaintWhite.setStrokeWidth(strokeWidth);
        strokePaintBackground.setStrokeWidth(strokeWidth - 4);
        startAngles = 0;
        for (int i = 0; i < colors.length; i++) {
            canvas.save();
            int sweepAngle = (i + 1) * 36;

            piePaint.setColor(colors[i]);
            drawProgress(canvas, sweepAngle);
            drawStrokeWhite(canvas, sweepAngle);
            startAngles += sweepAngle;
            canvas.restore();
        }
        startAngles = 0;
        for (int i = 0; i < colors.length; i++) {
            int sweepAngle = (i + 1) * 36;
            drawStrokeBackground(canvas, sweepAngle);
            startAngles += sweepAngle;
        }
        circlePaint.setStrokeWidth(strokeWidth);
        canvas.drawCircle(0, 0, this.radius + 2, circlePaint);
    }

    private void drawProgress(Canvas canvas, int sweepAngle) {
        // 绘制圆环
        RectF oval = new RectF(-radius, -radius, radius, radius);
        canvas.drawArc(oval, startAngles - 90, sweepAngle, true, piePaint); //
    }


    private void drawStrokeWhite(Canvas canvas, int sweepAngle) {
        Path path = new Path();
        RectF oval = new RectF(-radius, -radius, radius, radius);
        path.arcTo(oval, startAngles - 90, sweepAngle);
        path.lineTo(0, 0);
        path.close();
        canvas.drawPath(path, strokePaintWhite);
    }

    private void drawStrokeBackground(Canvas canvas, int sweepAngle) {
        int offsetX = (int) (Math.sin(Math.PI * (startAngles + sweepAngle) / 180) * radius);
        int offsetY = (int) (Math.cos(Math.PI * (startAngles + sweepAngle) / 180) * radius);
        Log.d("ProgressPieViewNew", "startAngles + sweepAngle = " + startAngles + sweepAngle);
        Log.d("ProgressPieViewNew", "offsetX = " + offsetX + "  offsetY = " + offsetY);
        canvas.drawLine(0, 0, offsetX, -offsetY, strokePaintBackground);
    }

    public void setSpace(int strokeWidth) {
        this.strokeWidth = strokeWidth;
        postInvalidate();
    }
}
